import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * Created by chenfeng(michaellele) on 2017/6/1.
 *
 * @email chenfeng@qingbeitt.com
 *
 * @description 金额转化类
 */
public class AmountConversion
{
    /**
     * 放大
     * @param fee 价格(单位元 小数点后只有两位到分)
     * @param alpha 放大倍数
     * **/
    public static String zoomIn(String fee, int alpha)
    {
        BigDecimal decimal = new BigDecimal(fee);
        return String.valueOf(decimal.multiply(new BigDecimal(String.valueOf(alpha))).intValue());
    }


    /**
     * 缩小
     * @param fee 放大的价格
     * @param alpha 缩小倍数
     * @return 金额 单位元
     * **/
    private static String zoomOut(BigInteger fee, int alpha)
    {
        BigInteger left = fee.divide(BigInteger.valueOf(alpha));

        BigInteger right = fee.mod(BigInteger.valueOf(alpha));
//        if(right.equals(BigInteger.ZERO))
//            return left.multiply(BigInteger.valueOf(100)).intValue();

        int len = 0;
        for(int i=0;;i++)
        {
            if(Math.pow(10,i)==alpha)
            {
                len = i;
                break;
            }
        }
        String feeStr = left + ".";
        if(! right.equals(BigInteger.ZERO) && String.valueOf(right).length()<len)
        {
            for(int i=0;i<len - String.valueOf(right).length();i++)
            {
                feeStr = feeStr + "0";
            }
        }
        feeStr = feeStr + right;
        return feeStr;
        // BigDecimal decimal = BigDecimal.valueOf(new Double(feeStr));
        // DecimalFormat f = new DecimalFormat("#,##0.00");
        // f.setRoundingMode(RoundingMode.HALF_UP);
        // System.out.println(f.format(new Double(feeStr)));//结果保留两位小数
        //最后乘以100化成分
        //int rs=  decimal.setScale(2,BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)).intValue()
        //return rs;
    }
    /**
     * @param  fee 单位为分
     * @param  alpha fee转化成元所需的倍数alpha
     * @return 返回单位元
     * **/
    public static String convert2RMB_YUAN(BigInteger fee, int alpha)
    {
        String feeStr =  zoomOut(fee,alpha);
        DecimalFormat f = new DecimalFormat("#,##0.00");
        f.setRoundingMode(RoundingMode.HALF_UP);
        return f.format(BigDecimal.valueOf(Double.parseDouble(feeStr)));//结果保留两位小数
    }
    public static String convert2RMB_YUAN(BigInteger fee, int alpha, boolean format)
    {
        if(format)
        {
            return convert2RMB_YUAN(fee, alpha);
        }

        String feeStr =  zoomOut(fee,alpha);
        DecimalFormat f = new DecimalFormat("0.00");
        f.setRoundingMode(RoundingMode.HALF_UP);
        return f.format( BigDecimal.valueOf(new Double(feeStr)));//结果保留两位小数
    }

    public static String convertAndCeiling2_YUAN(int fee)
    {
        return String.valueOf(CEILING(fee)/100);
    }

    /**
     * @param  fee 单位为分
     * @param  alpha fee转化成元所需的倍数alpha
     * @return 返回单位分
     * **/
    public static int convert2RMB_FEN(BigInteger fee,int alpha)
    {
        String feeStr =  zoomOut(fee,alpha);
        BigDecimal decimal = BigDecimal.valueOf(new Double(feeStr));
        return decimal.setScale(2,BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)).intValue();
    }

    /**
     * 四舍五入
     * @param fee
     * @param alpha
     * @return
     */
    public static int halfUp2RMB_FEN(Integer fee, int alpha)
    {
        String yuanStr = convert2RMB_YUAN(BigInteger.valueOf(fee), alpha, false);
        return Integer.parseInt(zoomIn(yuanStr, 100));
    }

    /**
     * 取整到百位
     * @param fee
     * @return
     */
    public static int CEILING(int fee)
    {
        int mod = fee % 10000;
        if(mod > 0)
        {
            fee = (fee/10000) * 10000 + 10000;
            return fee;
        }
        else
        {
            return fee;
        }
    }

    /**
     * 对整型红包金额进行处理
     * @param fee
     */
    public static String convertPacketAmount(int fee)
    {
        if(fee % 100 == 0)
        {
            return  String.valueOf(fee / 100);
        }
        else
        {
            return convert2RMB_YUAN(BigInteger.valueOf(fee), 100);
        }
    }

    /**
     * @param before_fee 元
     * @param div 2
     * @return 元
     * **/
    public static String divide(String before_fee, String div)
    {
        BigDecimal decimal = BigDecimal.valueOf(new Double(before_fee));

        DecimalFormat f = new DecimalFormat("0.00");
        f.setRoundingMode(RoundingMode.HALF_UP);
        return f.format(new Double(
                decimal.setScale(2,BigDecimal.ROUND_HALF_UP)
                        .divide(BigDecimal.valueOf(Integer.parseInt(div)))
                        .toString()));//结果保留两位小数
    }

    public static void main(String[] args)
    {
        /*int area = 121212;

        BigInteger bigArea = BigInteger.valueOf(area);
        BigDecimal bigDArea = BigDecimal.valueOf(1212.12);

        int unitFee = 12111;
        BigInteger bigUnitFee = BigInteger.valueOf(unitFee);
        BigDecimal bigDUnitFee = BigDecimal.valueOf(121.11);
        int month =12;

        System.out.println(bigDArea.multiply(bigDUnitFee).multiply(BigDecimal.valueOf(month)));

        System.out.println(bigArea.multiply(bigUnitFee).multiply(BigInteger.valueOf(month)));

        int total = area * unitFee * month;

        System.out.println(AmountConversion.zoomOut(bigArea.multiply(bigUnitFee).multiply(BigInteger.valueOf(month)),10000));
        System.out.println(AmountConversion.convert2RMB_YUAN(bigArea.multiply(bigUnitFee).multiply(BigInteger.valueOf(month)),10000));
        System.out.println(AmountConversion.convert2RMB_FEN(BigInteger.valueOf(300),100));
        System.out.println(AmountConversion.convert2RMB_YUAN(BigInteger.valueOf(300),100));

        System.out.println(AmountConversion.divide("1747.6668","2"));
        System.out.println(AmountConversion.halfUp2RMB_FEN(17744320,1000000));
        System.out.println(AmountConversion.zoomIn("1747.6",100));
        System.out.println(AmountConversion.CEILING(10304382));
        System.out.println(AmountConversion.convertAndCeiling2_YUAN(10304382));

        OneTimeEnums enums = OneTimeEnums.getByEndYearMonth(1527782400000L);
        System.out.print(enums.getDescription());*/
        /*System.out.println(AmountConversion.halfUp2RMB_FEN(14317*50,10000));
        System.out.println(AmountConversion.convert2RMB_YUAN(BigInteger.valueOf(7159),100));
        System.out.println(AmountConversion.halfUp2RMB_FEN(14317 * 50, 10000 ));*/
        System.out.println(AmountConversion.convert2RMB_FEN(BigInteger.valueOf(10000*80),10000));
        //System.out.println(AmountConversion.convert2RMB_YUAN(BigInteger.valueOf(AmountConversion.halfUp2RMB_FEN(14317 * 50, 10000 )),100));
        //System.out.println(convert2RMB_YUAN(BigInteger.valueOf(234755),10000));

    }
}
